﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Iacym.Repository.Mapping;
using Iacym.Entities;
using System.Data;

namespace Iacym.Repository
{
    public class CelulaRepository : MasterRepository, ICelulaRepository
    {
        public List<CELULA> GetAllFromCelulas()
        {
            var query = from c in context.CELULAs.Include("MINISTERIO")
                        select c;
            return query.ToList();
        }

        public List<CELULA> GetFromCelulasByCriterio(string criterio)
        {
            if (criterio != null && criterio != string.Empty && criterio != "")
            {
                var query = from c in context.CELULAs.Include("MINISTERIO")
                        where c.Descripcion.ToUpper().Contains(criterio) || c.MINISTERIO.Descripcion.ToUpper().Contains(criterio)
                        select c;

                return query.ToList();
            }
            else
            {
                var query = from c in context.CELULAs
                            select c;
                return query.ToList();
            }
        }

        public CELULA GetFromCelulaById(int id)
        {
            var query = from c in context.CELULAs.Include("MINISTERIO")
                        where c.Id.Equals(id)
                        select c;
            return query.SingleOrDefault();
        }



        public bool GetFromCelulaByMinisterioId(int idministerio)
        {
            var query = from c in context.CELULAs.Include("MINISTERIO")
                        where c.MinisterioId.Equals(idministerio)
                        select c;
            return query.Any();
        }

        public void AddCelula(CELULA celula)
        {

            context.Entry(celula.MINISTERIO).State = EntityState.Unchanged;

            context.CELULAs.Add(celula);
            context.SaveChanges();
        }

        public void UpdateCelula(CELULA celula)
        {
            context.Entry(celula).State = System.Data.EntityState.Modified;
            context.SaveChanges();
        }

        public void RemoveCelula(int id)
        {
            var existe = context.CELULAs.Where(c => c.Id.Equals(id)).SingleOrDefault();

            if (existe != null)
            {
                context.CELULAs.Remove(existe);
                context.SaveChanges();
            }
        }


        public bool ExistDuplicidadDescripcion(string descripcion, int id)
        {
            var query = from c in context.CELULAs
                        where c.Descripcion.ToUpper().CompareTo(descripcion.ToUpper()) == 0 && c.Id != id
                        select c;
            return query.Any();
        }


        public bool ExistId(string descripcion, int id)
        {
            var query = from c in context.CELULAs.Include("MINISTERIO")
                        where c.MinisterioId.Equals(id) && c.Descripcion == descripcion
                        select c;

            return query.Any();
        }

    }
}
